directives.directive('filter', ["$rootScope", "tools", "$timeout", "$routeParams", "tmp", "filterServ", function ($rootScope, tools, $timeout, $routeParams, tmp, filterServ) {
    return {
        restrict: 'A',
        scope: false,
        link: function (scope, element, attr) {
            var config = scope.config = scope.getCondition(attr.conditionid ? Number(attr.conditionid) : Number(attr.tmpid));
            var $container = $('.charts-container');
            var base = $container.width();
            var $ele = $(element);
            var events = {};

            if (attr.width) {
                config.width = Number(attr.width);
            }
            if (attr.height) {
                config.height = Number(attr.height);
            }

            // 非移动端模式下才是绝对定位,移动端下增加一个底部margin
            if (!config.mobileMode) {
                $ele.addClass('pa');
            } else {
                $ele.addClass('mb10');
                $ele.addClass('pr');
            }

            /*$ele.addClass('border').css({
             top: config.top <= 1 ? base * config.top + 'px' : config.top + 'px',
             left: config.left <= 1 ? base * config.left + 'px' : config.left + 'px',
             width: config.width <= 1 ? base * config.width + 'px' : config.width + 'px',
             height: config.height <= 1 ? base * config.height + 'px' : config.height + 'px'
             });*/

            $ele.addClass('border').css({
                width: config.width <= 1 ? base * config.width + 'px' : config.width + 'px',
                height: config.height <= 1 ? base * config.height + 'px' : config.height + 'px'
            });

            // 初始化
            if (!tools.mobileEnv()) {
                $ele.css({
                    top: config.top <= 1 ? base * config.top + 'px' : config.top + 'px',
                    left: config.left <= 1 ? base * config.left + 'px' : config.left + 'px'
                });
            }

            $ele.on('click', function (e) {
                e.stopPropagation();
            });

            var conditionTypes = {
                between: 'between',
                checkbox: 'in',
                radio: 'equal',
                date: 'between'
            };

            if (base * config.top + $ele.height() > $container.height() - 100) {
                $container.height($container.height() + 150);
            }

            // 字段恢复
            scope.recover_fields = function (flag) {
                $rootScope.$broadcast('FIELD_RECOVER_FROM_COND_EV', flag ? scope.config.column : null);
                $rootScope.$broadcast('SELECTED_TYPE_EV', scope.config.condType);
            }

            scope.fieldTitle = config.fieldTitle || '';

            // 增加模块
            scope.addPageCondition = function () {
                tools.get({
                    url: '/Ibm-Client/addPageCondition.htm',
                    data: {
                        conditionType: conditionTypes[config.condType],
                        columnId: scope.config.column.columnId,
                        tableType: $routeParams.tableType,
                        frontElementType: 'checkbox',
                        pageId: $routeParams.id,
                        tbId: $routeParams.tbId,
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            scope.getConditionData(scope.config.conditionId = resp.value.conditionId);
                            scope.fieldTitle = resp.value.fieldTitle;

                            if (!attr.conditionid) {
                                // tmp.data.conditions[config.condType].push(config);
                            }

                            scope.recover_fields(true);
                        }
                    }
                });
            }

            /*if (scope.config.column) {
             scope.addPageCondition();
             }*/

            // 右键菜单
            var menus = [
                {
                    name: '清空',
                    icon: 'eraser icon',
                    fn: function () {
                        scope.clearData();
                    }
                },
                {
                    name: '删除',
                    icon: 'trash icon',
                    fn: function () {
                        if (scope.config.conditionId) {
                            events.CONFIRM_EV_OFF = $rootScope.$broadcast('CONFIRM_EV', {
                                title: '组件删除提醒',
                                content: '组件删除之后不可恢复,确定删除吗?',
                                btns: {
                                    yes: {
                                        text: "确定删除",
                                        fn: function () {
                                            filterServ.deletePageCondition(config);
                                            scope.clearCondition();
                                        }
                                    },
                                    no: {
                                        text: '取消',
                                        fn: function () {

                                        }
                                    }
                                }
                            });
                        } else {
                            scope.delCondition(scope.config.tmpId);
                            $rootScope.$broadcast('CONFIRM_OFF_EV');
                            $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                        }
                    }
                }
            ];

            // 右键操作
            element[0].addEventListener('contextmenu', function (e) {
                if (e.button == 2) {
                    $rootScope.$broadcast('CONTEXTMENU_EV', e.pageX, e.pageY, menus);
                    e.preventDefault();
                }
                e.stopPropagation();
                return false;
            });

            // 监听组件字段增加
            events.COLUMN_ADD_EV_OFF = $rootScope.$on('CONDITION_ADD_EV', function (e, field) {
                if (scope.selected) {
                    // 清理之前发出的筛选
                    scope.clearCondition();

                    scope.config.column = field;
                    scope.addPageCondition();
                }
            });

            // 监听组件字段减少
            events.CONDITION_REDUCE_EV_OFF = $rootScope.$on('CONDITION_REDUCE_EV', function (e, columnId) {
                if (scope.selected) {
                    scope.config.column = null;
                    if (scope.config.condType == 'checkbox' || scope.config.condType == 'radio') {
                        scope.datas.length = 0;
                        scope.fieldTitle = '';
                    } else if (scope.config.condType == 'date') {
                        scope.fieldTitle = '';
                        if (scope.dates[0]) {
                            scope.dates[0].shown = false;
                        }
                    } else if (scope.config.condType == 'between') {
                        scope.fieldTitle = '';
                        $ele.find('.slider-container').hide();
                    }
                }
            });

            //
            events.UPDATE_COND_EV_OFF = $rootScope.$on('UPDATE_COND_EV', function (e, columnId, columnDesc) {
                if (scope.config.column.columnId === columnId) {
                    scope.config.column.columnDesc = columnDesc;
                }
            });

            // 保存
            events.SAVE_EV_OFF = $rootScope.$on('SAVE_EV', function () {
                var base = $container.width();

                config.width = $ele.width() / base;
                config.height = $ele.height() / base;
                config.left = $ele.position().left / base;
                config.top = $ele.position().top / base;
                config.fieldTitle = scope.fieldTitle;
                config.selected = false;

                for (var i in config.column) {
                    if (config.column[i] === null) {
                        config.column[i] = '';
                    }
                }
            });

            // 删除
            events.DEL_COND_EV_OFF = $rootScope.$on('DEL_COND_EV', function () {
                filterServ.deletePageCondition(config);
            });

            scope.$on('$destroy', function () {
                for (var i in events) {
                    if (i.indexOf('_OFF') != -1 && events[i] instanceof Function) {
                        events[i]();
                    }
                }
            });
        }
    }
}]);
services.factory('filterServ', ["$rootScope", "tools", "$timeout", "$routeParams", function ($rootScope, tools, $timeout, $routeParams) {
    return {
        deletePageCondition: function (config, cb) {
            if (!config) {
                return;
            }
            if (!config.conditionId && config.tmpId) {
                return $rootScope.delCondition(config.conditionId);
            }
            tools.get({
                url: '/Ibm-Client/deletePageCondition.htm',
                data: {
                    conditionId: config.conditionId,
                    pageId: $routeParams.id
                },
                succ: function (resp) {
                    if (resp.success) {
                        cb && cb();
                        $rootScope.delCondition(config.conditionId);

                        $rootScope.$broadcast('FIELD_RECOVER_FROM_COND_EV', null);
                        $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                        $rootScope.$broadcast('CONFIRM_OFF_EV');
                    }
                }
            });
        }
    }
}]);